package com.wisemoresystem.seal.service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_seal_use_apply;
import com.wisemoresystem.seal.model.t_seal_use_apply_verify;
import com.wisemoresystem.seal.model.t_wkflow;
import com.wisemoresystem.seal.util.SpringTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 工作流流程
 * 
 * @author 冯晓东
 *
 */
@Service
public class WkflowService extends BaseService {

	public static void main(String[] args) {
		WkflowService s = SpringTools.getBean(WkflowService.class);

	}

	public List<Map<String, Object>> selectWkflowList() {
		String sql = "select * from t_wkflow t where t.status=0 and t.bank_id=?";
		List<Map<String, Object>> list = jt.queryForList(sql,
				WebTools.getCurrentBankId());
		return list;
	}

	@Transactional
	public void makeWkflow(String apply_id, String wkflow_id) {
		// TODO 根据工作流，生成审批报表

		t_seal_use_apply apply = this.findObjById(apply_id,
				t_seal_use_apply.class);

		t_wkflow wkflow = this.findObjById(wkflow_id, t_wkflow.class);
		String job_codes = wkflow.getJob_codes();

		if (ChkTools.isNull(job_codes) || ChkTools.isNull(job_codes.trim())) {
			apply.setPass_status("审批通过");
			apply.setSeal_status("待盖章");
			this.updateObj(apply);
			return;
		}

		apply.setPass_status("审批中");
		this.updateObj(apply);

		String[] jobCodesArr = job_codes.split(",");
		for (int i = 0; i < jobCodesArr.length; i++) {
			String jobCode = jobCodesArr[i];
			t_seal_use_apply_verify vy = new t_seal_use_apply_verify();
			vy.setBank_id(WebTools.getCurrentBankId());
			vy.setApply_id(apply.get_id());
			vy.setJob_code(jobCode);
			vy.setVorder(i);
			if (i == 0) {
				vy.setVerify("待审批");
			} else {
				vy.setVerify("未启动");
			}
			this.saveObj(vy);
		}

	}// #makeWkflow

	@Transactional
	public void verify(String verify_id, String verify_end,
			String president_opinion) {
		// TODO 执行审批
		t_seal_use_apply_verify verify = this.findObjById(verify_id,
				t_seal_use_apply_verify.class);
		verify.setVerify(verify_end);
		verify.setVerify_date(new Timestamp(System.currentTimeMillis()));
		verify.setVerify_user_id(WebTools.getCurrentUserId());
		this.updateObj(verify);

		t_seal_use_apply apply = this.findObjById(verify.getApply_id(),
				t_seal_use_apply.class);
		apply.setPresident_opinion(president_opinion);
		
		String hql = "from t_seal_use_apply_verify t where t.apply_id=? and t.vorder>? order by t.vorder asc";
		List<t_seal_use_apply_verify> list = (List<t_seal_use_apply_verify>) ht
				.find(hql, verify.getApply_id(), verify.getVorder());

		if ("未通过".equals(verify)) {
			for (t_seal_use_apply_verify v : list) {
				v.setVerify("已作废");
				v.setVerify_date(new Timestamp(System.currentTimeMillis()));
				this.updateObj(v);
			}
			apply.setPass_status("审批不通过");
			this.updateObj(apply);

			return;
		}
		if (list.size() == 0) {
			// 草稿 or 审批中 or 审批不通过 or 审批通过
			apply.setPass_status("审批通过");
			apply.setSeal_status("待盖章");
			this.updateObj(apply);
			return;
		}
		t_seal_use_apply_verify v = list.get(0);
		v.setVerify("待审批");
		this.updateObj(v);

	}// #verify
}
